package com.cwn.wethink.common.web;

import com.cwn.wethink.comm.enums.ChannelEnum;

import javax.servlet.http.HttpServletRequest;

public class ChannelUtil {

    /**
    * 实现功能描述：帮助类不应该有公共构造函数，帮助类不宜实例化，且应该有一个如下的私有构造方法
    * @return
    */
    private ChannelUtil() {}

    public static final String API_DEVELOPER_MOBILE = "mobile";
    public static final String API_DEVELOPER_PC = "pc";
    public static final String API_DEVELOPER_WEIXIN = "weixin";
    public static final String API_DEVELOPER_API = "api";

    private static String channelParamKey = "developer";
    private static String unknown = "unknown";
    private static String tokenKey = "token";

    protected static final String[] mobileAgents = {"iphone", "android", "phone", API_DEVELOPER_MOBILE, "wap", "netfront", "java", "opera mobi",
            "opera mini", "ucweb", "windows ce", "symbian", "series", "webos", "sony", "blackberry", "dopod",
            "nokia", "samsung", "palmsource", "xda", "pieplus", "meizu", "midp", "cldc", "motorola", "foma",
            "docomo", "up.browser", "up.link", "blazer", "helio", "hosin", "huawei", "novarra", "coolpad", "webos",
            "techfaith", "palmsource", "alcatel", "amoi", "ktouch", "nexian", "ericsson", "philips", "sagem",
            "wellcom", "bunjalloo", "maui", "smartphone", "iemobile", "spice", "bird", "zte-", "longcos",
            "pantech", "gionee", "portalmmm", "jig browser", "hiptop", "benq", "haier", "^lct", "320x320",
            "240x320", "176x220", "w3c ", "acs-", "alav", "alca", "amoi", "audi", "avan", "benq", "bird", "blac",
            "blaz", "brew", "cell", "cldc", "cmd-", "dang", "doco", "eric", "hipt", "inno", "ipaq", "java", "jigs",
            "kddi", "keji", "leno", "lg-c", "lg-d", "lg-g", "lge-", "maui", "maxo", "midp", "mits", "mmef", "mobi",
            "mot-", "moto", "mwbp", "nec-", "newt", "noki", "oper", "palm", "pana", "pant", "phil", "play", "port",
            "prox", "qwap", "sage", "sams", "sany", "sch-", "sec-", "send", "seri", "sgh-", "shar", "sie-", "siem",
            "smal", "smar", "sony", "sph-", "symb", "t-mo", "teli", "tim-", "tsm-", "upg1", "upsi", "vk-v",
            "voda", "wap-", "wapa", "wapi", "wapp", "wapr", "webc", "winw", "winw", "xda", "xda-",
            "Googlebot-Mobile"};

    public static ChannelEnum getChannel(String developer, String userAgent) {
        final boolean forceMobile = API_DEVELOPER_MOBILE.equals(developer);
        ChannelEnum channelEnum = ChannelEnum.PC;
        if (userAgent != null) {
            if (userAgent.contains("MicroMessenger")) {
                channelEnum = ChannelEnum.WECHAT;
            } else if (forceMobile) {
                //严格的说还有第4种方式，pc访问移动端
                channelEnum = ChannelEnum.MOBILE;
            } else {
                for (String mobileAgent : mobileAgents) {
                    if (userAgent.toLowerCase().indexOf(mobileAgent) >= 0) {
                        channelEnum = ChannelEnum.MOBILE;
                        break;
                    }
                }
            }
        }
        return channelEnum;
    }


    /**
     * 获取id地址
     *
     * @param request unknown
     * @return
     */
    public static String getIpAddress(HttpServletRequest request) {
        // 获取请求主机IP地址,如果通过代理进来，则透过防火墙获取真实IP地址
        String ip = request.getHeader("X-Forwarded-For");
        if (checkIP(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
            if (checkIP(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (checkIP(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (checkIP(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (checkIP(ip)) {
                ip = request.getRemoteAddr();
            }
        } else if (ip.length() > 15) {
            String[] ips = ip.split(",");
            for (int index = 0; index < ips.length; index++) {
                String strIp = ips[index];
                if (!("unknown".equalsIgnoreCase(strIp))) {
                    ip = strIp;
                    break;
                }
            }
        }
        return ip;
    }

    /**
    * 实现功能描述：校验IP
    * @param ip IP
    * @return
    */
    private static boolean checkIP(String ip) {
        return ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip);
    }

    public static String getChannelParamKey() {
        return channelParamKey;
    }

    public static String getTokenKey() {
        return tokenKey;
    }
}
